package ${bizPackage}.service.${tablePrefix}.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${bizPackage}.service.${tablePrefix}.${entityName}Service;
import ${bizPackage}.mapper.${tablePrefix}.${entityName}Mapper;
import ${commonPackage}.entity.${tablePrefix}.${entityName};
import ${commonPackage}.dto.${tablePrefix}.${entityName}AddDTO;
import ${commonPackage}.dto.${tablePrefix}.${entityName}ModifyDTO;
import ${commonPackage}.dto.${tablePrefix}.${entityName}PageDTO;
import ${commonPackage}.vo.${tablePrefix}.${entityName}DetailVO;
import ${commonPackage}.vo.${tablePrefix}.${entityName}PageVO;
import ${commonPackage}.vo.PageVO;
import ${commonPackage}.convert.${tablePrefix}.${entityName}Convert;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ${tableComment} 服务实现
 *
 * @author ${username}
 * @date ${createTime}
 */
@Service
@RequiredArgsConstructor
public class ${entityName}ServiceImpl extends ServiceImpl<${entityName}Mapper, ${entityName}>
    implements ${entityName}Service {

    @Override
    public void addOne(${entityName}AddDTO ${entityNameLowerFirst}AddDTO) {
        ${entityName} ${entityNameLowerFirst} = ${entityName}Convert.INSTANCE.addDTO2Entity(${entityNameLowerFirst}AddDTO);
    	this.save(${entityNameLowerFirst});
    }

    @Override
    public void deleteById(Long id) {
        removeById(id);
    }

    @Override
    public void modifyById(Long id, ${entityName}ModifyDTO ${entityNameLowerFirst}ModifyDTO) {
        ${entityName} ${entityNameLowerFirst} = ${entityName}Convert.INSTANCE.modifyDTO2Entity(${entityNameLowerFirst}ModifyDTO);
        ${entityNameLowerFirst}.setId(id);
        this.updateById(${entityNameLowerFirst});
    }

    @Override
    public ${entityName}DetailVO detailById(Long id) {
        ${entityName} ${entityNameLowerFirst} = this.getById(id);
        return ${entityName}Convert.INSTANCE.entity2DetailVO(${entityNameLowerFirst});
    }

    @Override
    public PageVO<${entityName}PageVO> queryPage(${entityName}PageDTO ${entityNameLowerFirst}PageDTO) {
        Long page = ${entityNameLowerFirst}PageDTO.getPage();
        Long size = ${entityNameLowerFirst}PageDTO.getSize();
        Page<${entityName}> ${entityNameLowerFirst}Page = page(
				lambdaQuery()
						.orderByDesc(${entityName}::getUpdatedAt)
						.page(Page.of(page, size))
		);
		List<${entityName}PageVO> pageVOS = ${entityName}Convert.INSTANCE.entityList2PageVOList(${entityNameLowerFirst}Page.getRecords());
		return PageVO.of(${entityNameLowerFirst}Page, pageVOS);
    }
}
